<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\admin\controller;

use Mscore\Core\Controller;
use Mscore\Core\Lib\Captcha;
use app\admin\model\Login;
use Mscore\Core\Lang;

class LoginController extends Controller
{
    private $language;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
    }

    public function index(){
        $this->display("login.html"); 
    }

    /**
     * 登录
     * @param array $login 
     * @param array $where 
    */
    public function login(){
        $lang = post('lang',false); $lang = isset($lang)?$lang:'';
        $info = (new Login)->userAuth(post('username',false),post('passwd',false),$lang,1);
        $this->echoJson($info);
    }
    
    /**
     * 登出
     * @param array $logout 
     * @param array $where 
    */
    public function logout(){
        $token = post('token',true); 
        $token = isset($token)?$token:'';
        $login = (new Login)->logout($token);
        if($login['code']==200){
                $authset=config('session');
                switch (true) {
                    case $authset["usesession"]==true: //清除session
                        session_start();
                        $_SESSION["token"]='';unset($_SESSION);
                        break;
                    case $authset["usecookie"]==true: //清除cookie
                        setcookie("token", '', time()-3600);
                        break;
                    default:
                        Header("Location:".config('server.login_url'));
                        break;
                }
            Header("Location:".config('server.login_url'));
        }
    }

    /**
     * 输出验证码
     * @param $img
    */
    public function doimg(){
        $img =(new Captcha())->doimg();
    }

    /**
     * 获得验证码 getCaptcha
     * @param $verifyCode,$expiretime
    */
    public function getCaptcha(){
        $loginset=config('session');
        switch (true) {
            case $loginset["usesession"]==true:
                session_start();
                $VerifyCode=$_SESSION["verifyCode"];
                if($_SESSION['expiretime']>time()){
                    unset($_SESSION);
                    echo($VerifyCode);
                }else{
                    echo '重新获取';    
                }
                break;
            case $loginset["usecookie"]==true:
                $VerifyCode=$_COOKIE["verifyCode"];
                echo($VerifyCode);
                break;
            default:
                echo '重新获取'; 
                break;
        }   
    }

    /**
     * 切换语言
     * @param array $login 
     * @param array $where 
    */
    public function setlanguage(){
        $lang = post('lang',false); $lang = isset($lang)?$lang:config('langs.default');
        if (!setcookie("language", $lang, time() + 365*24*60*60,'/')){
            $info=['code'=>100,'info'=>"set failed"];
            $this->echoJson($info);
        }
        $info=['code'=>200];
        $this->echoJson($info);
    }
    
}